package com.atguigu.gulimall.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description :线程池的配置
 * @Author : mayp
 * @Date: 2020-10-29 21:17
 */
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class) 由于ThreadPoolConfigProperties类中使用了@Component注解，所以这里不需要指定
@Configuration
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool){
        /**
         * int corePoolSize,核心线程池大小
         * int maximumPoolSize,最大线程池
         * long keepAliveTime,线程池存活时间
         * TimeUnit unit, 时间单位
         * BlockingQueue<Runnable> workQueue,阻塞队列长度（最多存储多少个异步任务）
         * ThreadFactory threadFactory,默认的线程工厂
         * RejectedExecutionHandler handler 拒绝策略
         */
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(pool.getCoreSize(),
                                                                        pool.getMaxSize(),
                                                                        pool.getKeepAliveTime(),
                                                                        TimeUnit.SECONDS,
                                                                        new LinkedBlockingDeque<>(), Executors.defaultThreadFactory(),
                                                                        new ThreadPoolExecutor.AbortPolicy());
        return threadPoolExecutor;
    }
}
